﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	[ToolboxData("<{0}:MyListView runat=\"server\"></{0}:MyListView>")]
	public class MyListView : MyListControl, INamingContainer {
		/// <summary>
		/// 
		/// </summary>
		public class ItemContainer : TemplateControl, IDataItemContainer, INamingContainer {
			private object m_dataItem;

			private int m_dataItemIndex;

			private int m_displayIndex;

			/// <summary>
			/// 
			/// </summary>
			/// <param name="dataItem"></param>
			/// <param name="index"></param>
			/// <param name="displayIndex"></param>
			public ItemContainer(object dataItem, int index, int displayIndex)
				: base() {
				m_dataItem = dataItem;
				m_dataItemIndex = index;
				m_displayIndex = displayIndex;
			}

			/// <summary>
			/// 
			/// </summary>
			/// <param name="writer"></param>
			protected override void Render(HtmlTextWriter writer) {
				foreach(Control ctl in base.Controls) {
					ctl.RenderControl(writer);
				}
			}

			#region IDataItemContainer

			object IDataItemContainer.DataItem {
				get { return m_dataItem; }
			}

			int IDataItemContainer.DataItemIndex {
				get { return m_dataItemIndex; }
			}

			int IDataItemContainer.DisplayIndex {
				get { return m_displayIndex; }
			}

			#endregion
		}

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[Category("Appearance")]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemContainer), System.ComponentModel.BindingDirection.TwoWay)]
		[TemplateInstance(TemplateInstance.Single)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate EmptyDataTemplate { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[Category("Appearance")]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemContainer), System.ComponentModel.BindingDirection.TwoWay)]
		[TemplateInstance(TemplateInstance.Single)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate SeperatorTemplate { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[Category("Appearance")]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemContainer), System.ComponentModel.BindingDirection.TwoWay)]
		[TemplateInstance(TemplateInstance.Single)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate ItemTemplate { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Browsable(false)]
		[Category("Appearance")]
		[DefaultValue("")]
		[TemplateContainer(typeof(ItemContainer), System.ComponentModel.BindingDirection.TwoWay)]
		[TemplateInstance(TemplateInstance.Single)]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public ITemplate AlternateItemTemplate { get; set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		protected override void RenderEmpty(HtmlTextWriter writer) {
			if (EmptyDataTemplate != null) {
				var itemControl = new ItemContainer(null, 0, 0);

				EmptyDataTemplate.InstantiateIn(itemControl);

				itemControl.RenderControl(writer);
			} else {
				base.RenderEmpty(writer);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		/// <param name="index"></param>
		protected override void RenderListItem(HtmlTextWriter writer, MyListItem item, int index) {
			var itemControl = new ItemContainer(item, index, index);

			if (index % 2 == 0) {
				if (ItemTemplate != null) {
					ItemTemplate.InstantiateIn(itemControl);
				}
			} else {
				if (AlternateItemTemplate != null) {
					AlternateItemTemplate.InstantiateIn(itemControl);
				} else if (ItemTemplate != null) {
					ItemTemplate.InstantiateIn(itemControl);
				}
			}

			Controls.Add(itemControl);
			itemControl.DataBind();

			itemControl.RenderControl(writer);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="index"></param>
		protected override void RenderSeperator(HtmlTextWriter writer, int index) {
			if (SeperatorTemplate != null) {
				var itemControl = new ItemContainer(null, 0, 0);

				SeperatorTemplate.InstantiateIn(itemControl);

				itemControl.RenderControl(writer);
			} else {
				base.RenderSeperator(writer, index);
			}
		}
	} // end of MyListView.
}
